package com.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.entity.City;
import com.hy.mapper.CityMapper;
import com.hy.service.CityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements CityService {

    @Override
    public List<City> getAllCities() {
        LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(City::getDeleteFlag, 0)
               .orderByAsc(City::getCitiesName);
        return list(wrapper);
    }

    @Override
    public City getCityById(Long id) {
        LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(City::getId, id)
               .eq(City::getDeleteFlag, 0);
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public boolean addCity(City city) {
        city.setDeleteFlag(0);
        city.setCreateTime(LocalDateTime.now());
        city.setUpdateTime(LocalDateTime.now());
        return save(city);
    }

    @Override
    @Transactional
    public boolean updateCity(City city) {
        city.setUpdateTime(LocalDateTime.now());
        return updateById(city);
    }

    @Override
    @Transactional
    public boolean deleteCity(Long id) {
        LambdaUpdateWrapper<City> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(City::getId, id)
                    .set(City::getDeleteFlag, 1)
                    .set(City::getUpdateTime, LocalDateTime.now());
        return update(updateWrapper);
    }
} 